package com.ai.demo.service;

import io.modelcontextprotocol.client.McpClient;
import io.modelcontextprotocol.client.McpSyncClient;
import io.modelcontextprotocol.client.transport.ServerParameters;
import io.modelcontextprotocol.client.transport.StdioClientTransport;
import io.modelcontextprotocol.spec.McpSchema;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.mcp.SyncMcpToolCallbackProvider;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.util.MimeTypeUtils;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class McpClientServer {

    private final McpSyncClient mcpClient;

    @Autowired
    private OpenAiChatModel baseChatModel;

    @Autowired
    private OpenAiApi baseOpenAiApi;


    public McpClientServer(McpSyncClient mcpClient) {
        this.mcpClient = mcpClient;
    }

    public Flux<String> fetchHotNewsByChatClient(String userInput) {
        OpenAiChatModel openAiChatModel = baseChatModel.mutate()
                .openAiApi(baseOpenAiApi)
                .defaultOptions(OpenAiChatOptions.builder().model("deepseek-chat").temperature(0.7).build())
                .build();

        var chatClient = ChatClient.builder(openAiChatModel)
                .defaultToolCallbacks(new SyncMcpToolCallbackProvider(mcpClient))
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(MessageWindowChatMemory.builder().build()).build())
                .build();

        return chatClient.prompt(userInput) // 用户输入
                .stream() // 获取AI回复
                .content();
    }

    public String fetchHotNews() {
        // 创建工具请求参数
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("sources", Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9)); // 获取 热门话题
        // 创建 CallToolRequest
        McpSchema.CallToolRequest request = new McpSchema.CallToolRequest("get_hot_news", arguments);
        // 调用 callTool 方法
        McpSchema.CallToolResult result = mcpClient.callTool(request);
        // 打印结果
        System.out.println("Hot News from   " + result);
        // 取第一个文本内容（假设返回的是文本）
        String text = "";
        if (result.content() != null && !result.content().isEmpty()) {
            McpSchema.Content content = result.content().get(0);
            if (content instanceof McpSchema.TextContent textContent) {
                text = textContent.text();
            } else {
                text = content.toString(); // 兜底
            }
        }
        return text;
    }


}
